package cn.edu.javaweb.userServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.javaweb.utils.DBUtil;



@WebServlet("/ForumPersonalUpdateServlet")
public class ForumPersonalUpdateServlet extends HttpServlet {
	DBUtil db;
	private static final long serialVersionUID = 1L;
    
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
		String topic = request.getParameter("topic");
		String part = request.getParameter("part");
		String qid = request.getParameter("qid");
		String question = request.getParameter("question");
		db = new DBUtil();  //new的时候在调用（工具类）构造函数（初始化），实现数据库的搭建
		
		if(id!=null){
			String SQL = "update forum set topic='"+topic+"',part='"+part+"' where id='"+id+"'";
			System.out.print(SQL);
			int result=db.update(SQL);  //调用（工具类）update方法，result来传递结果
			response.setContentType("text/html;charset=utf-8");
		    PrintWriter out=response.getWriter();
		    if(result>0){
		    	out.println("<script language=javascript>alert('保存成功');window.location='ForumPersonalServlet';</script>");
		    }else{
		    	out.println("<script language=javascript>alert('保存失败');window.location='ForumPersonalServlet';</script>");
			}
		}else if(qid!=null ){
			String SQL2 = "update question set question='"+question+"' where id='"+qid+"'";
			System.out.print(SQL2);
			int result2=db.update(SQL2);  //调用（工具类）update方法，result来传递结果
			response.setContentType("text/html;charset=utf-8");
		    PrintWriter out=response.getWriter();
		    if(result2>0){
		    	out.println("<script language=javascript>alert('保存成功');window.location='ForumPersonalServlet';</script>");
		    }else{
		    	out.println("<script language=javascript>alert('保存失败');window.location='ForumPersonalServlet';</script>");
			}
		}
		
	}

}
